setwd("E:\\5hmc_file\\2_5hmc_yjp_bam\\ASM")
dir1="./bayes_pvalue_beta0/"
dir2="./bayes_BF/"
library(ggplot2)
library(scales)
library(RColorBrewer)
group1=c("X2B_X1T","M8_M7","M6_M5","M2_M1","M48_M47","M50_M49","M28_M27","M30_M29","M26_M25","M35_M36","M18_M17","M20_M19","M22_M21","M40_M39")
i=1
for(i in 1:length(group1)){
fn1=paste0(dir1,group1[i],".bayes_p.txt")
file1=read.table(fn1,head=T,sep = "\t")
file1$unitID=paste(file1$chrom,file1$position,sep=":")
file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)

fn2=paste0(dir2,group1[i],".bayes_factor.txt")
file2=read.table(fn2,head=T,sep="\t")
file2$unitID=paste(file2$chrom,file2$position,sep=":")
file2=data.frame(unitID=file2$unitID,BayesFactor=file2$BayesFactor)

file=merge(file2,file1,by="unitID")
file=file[file$BayesFactor>1,]
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$normal_bayes_pvalue<0.05,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$normal_bayes_pvalue<0.05,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$tumor_bayes_pvalue<0.05,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$tumor_bayes_pvalue<0.05,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
test=data.frame(table(file$pattern))
rtfn=paste("./20201102pie/",group1[i],".pattern_statis.csv",sep="")
write.csv(test,rtfn,quote=F,row.names = F)
mylabel=factor(test$Var1,levels = c("normal_down-tumor_nosig","normal_up-tumor_nosig","normal_nosig-tumor_up","normal_down-tumor_up","normal_up-tumor_down","normal_down-tumor_down","normal_up-tumor_up","normal_nosig-tumor_down"))

p1=ggplot(test,aes(x="",y=Freq,fill=mylabel))+
  geom_bar(stat="identity")+
  coord_polar("y",start=1) + 
  theme_minimal()+
  theme(axis.title=element_blank(),
        axis.ticks=element_blank(),
        axis.text = element_blank(),legend.position = "none",
        legend.title = element_blank())+scale_fill_brewer(palette="Set2")
  
fileMF=paste("./20201102pie/",group1[i],".png",sep="")
png(fileMF,width = 450,height = 350)
print(p1)
dev.off()
}

